﻿$(document).ready(function () {
    $('table').addClass('tablesorter');




    (function ($) {
        $.fn.tablePager = function (options) {
            this.each(function () {
                /* We wrap the table with a div, so that we can use that div as reference point for all other functions... neat!
                */
                $(this).wrap("<div>");

                /*	Default values
                */
                var defaults = {
                    offset: 0, //where to start
                    limit: 15, // how many rows each page
                    placeTop: false // place pager links over the table?
                };

                var options = $.extend(defaults, options);

                /*	Plugin variables
                */
                var parent = $(this).parent();
                var table = $("table", parent);
                var rows = $("tbody > tr", table);
                var totalItems = rows.length;
                var totalPages = Math.ceil((totalItems / options.limit));
                var currentPage = 1;

                /*	If the total amount of rows is bigger than the defined limit for each page, we do anything;
                */
                if (totalItems > options.limit) {
                    /*	Did we have more than 1 page?, if yes, create some page links ;-)
                    */
                    var pages = "";
                    if (totalPages > 1) {
                        for (var i = 1; i <= totalPages; i++) {
                            pages += "<li><a href='javascript://' class='page' id='" + i + "'>" + i + "</a></li>";
                        }
                    }

                    /*	HTML Layout for the links
                    */
                    var tb = $("<div class='pagerLinks'>\
						   		<ul>\
						   		<li><a href='javascript://' class='previousPage'>Previous</a></li>\
								" + pages + "\
								<li><a href='javascript://' class='nextPage'>Next</a></li>\
								</ul>\
							</div>");

                    /*	Appending it to the DOM
                    */
                    if (options.placeTop) {
                        table.before(tb);
                    }
                    else {
                        table.after(tb);
                    }

                    /*	And then we create functions for the links
                    */
                    $(".previousPage", parent).click(
					function () {
					    options.offset = (options.offset == 0) ? (totalItems - options.limit) : (options.offset - options.limit);
					    currentPage = (currentPage == 1) ? totalPages : currentPage - 1;
					    parent.renderTable();
					}
				);

                    $(".nextPage", parent).click(
					function () {
					    options.offset = ((options.offset + options.limit) == totalItems) ? 0 : (options.offset + options.limit);
					    currentPage = (currentPage == totalPages) ? 1 : currentPage + 1;
					    parent.renderTable();
					}
				);

                    $(".page", parent).each
				(
				 	function () {
				 	    $(this).click(
							function () {
							    options.offset = (this.id - 1) * options.limit;
							    currentPage = currentPage - (currentPage - this.id);
							    parent.renderTable();
							}
						);
				 	}
				);
                }

                /*	And finally we try to sort out witch rows should be visible ;-)
                */
                parent.renderTable = function () {
                    var currentItem = 0;

                    $(".pagerLinks *", parent).removeClass("active");
                    $("#" + currentPage + "", parent).addClass("active");

                    /*	So, if we are at last page we hide the next link ;-)
                    */
                    if (currentPage == totalPages) {
                        $(".nextPage", parent).css("visibility", "hidden");
                    }
                    else {
                        $(".nextPage", parent).css("visibility", "visible");
                    }

                    /*	If we are at first page we hide the prev link
                    */
                    if (currentPage == 1) {
                        $(".previousPage", parent).css("visibility", "hidden");
                    }
                    else {
                        $(".previousPage", parent).css("visibility", "visible");
                    }

                    /*	Run through all rows and check what we want to show
                    */
                    rows.each
				(
					function () {
					    obj = $(this);

					    if (currentItem >= options.offset && currentItem < (options.offset + options.limit)) {
					        obj.show();
					    }
					    else {
					        obj.hide();
					    }

					    currentItem++;
					}
				);
                }

                /*	Initialize on page load
                */
                parent.renderTable();
            });
        };
    })(jQuery);

    $('table').tableFilter();




    $("table").tablePager({ offset: 10, limit: 10, placeTop: true });


    $("table").tablesorter({ widthFixed: false, widgets: ['zebra'] });

});